/**
 * 后台管理系统路由配置
 */
// module.exports = function(backHomeApp) {
// 整个工程的主模块，依赖ngRoute子模块（第三方插件）
var backHomeApp = angular.module("backHomeApp", ['ui.router', 'ui.bootstrap','angularFileUpload']);
//权限控制
backHomeApp.run(['$rootScope', '$location','infoService',
    function($rootScope, $location,infoService) {
        $rootScope.$on('$stateChangeSuccess', function(evt, next, current) {
            // 第三级权限
            /*var threeRoot = function(next_url) {
                var arr = ["/member/newGraphic"],
                    result = false;
                for (var i = 0; i < arr.length; i++) {
                    if (next_url == arr[i]) {
                        result = true;
                        break;
                    }
                }
                return result;
            }*/
            var is_can = false;
            //判断是否加载权限菜单
            if (!$rootScope.mencs) {
                infoService.myPrivilegeList().then(function(data) {
                if (data.children.length == 0) {
                    $location.path("/home");
                    return;
                }
                /*if (threeRoot(next.url)) {
                    is_can = true;
                }*/
                $rootScope.datas = data.children;
                /*写死数据*/
                //右侧导航条目
                /*$rootScope.datas = [{
                    id: "user",
                    children: [],
                    icon: "glyphicon glyphicon-user",
                    name: "客户管理",
                    url: "customerManage",
                    type: '1',
                    paixu: 1
                }, {
                    id: "report",
                    children: [{
                        id: "brand_sta",
                        name: "品牌月统计",
                        url: "brandStatistics",
                        type: '2',
                        paixu: 1,
                        parent: "report"
                    }, {
                        id: "store_sta",
                        name: "店面月统计",
                        url: "storeStatistics",
                        type: '2',
                        paixu: 2,
                        parent: "report"
                    }, {
                        id: "sale_sta",
                        name: "销售月统计",
                        url: "saleStatistics",
                        type: '2',
                        paixu: 3,
                        parent: "report"
                    }],
                    icon: "glyphicon glyphicon-stats",
                    name: "报表管理",
                    type: '1',
                    paixu: 2
                }, {
                    id: "basic_data",
                    children: [{
                        id: "brand_manage",
                        name: "品牌管理",
                        url: "brandManage",
                        type: '2',
                        paixu: 1,
                        parent: "basic_data"
                    }, {
                        id: "car_manage",
                        name: "车型管理",
                        url: "carManage",
                        type: '2',
                        paixu: 2,
                        parent: "basic_data"
                    }, {
                        id: "store_manage",
                        name: "门店管理",
                        url: "storeManage",
                        type: '2',
                        paixu: 3,
                        parent: "basic_data"
                    }, {
                        id: "salesman_manage",
                        name: "销售员管理",
                        url: "salesmanManage",
                        type: '2',
                        paixu: 4,
                        parent: "basic_data"
                    }],
                    icon: "glyphicon glyphicon-filter",
                    name: "基础数据维护",
                    type: '1',
                    paixu: 3
                }, {
                    id: "system_manage",
                    children: [{
                            id: "user_manage",
                            name: "用户管理",
                            url: "userManage",
                            type: '2',
                            paixu: 1,
                            parent: "system_manage"
                        }
                        /*,{
                                        id: "role_manage",
                                        name: "角色管理",
                                        url: "roleManage",
                                        type: '2',
                                        paixu: 2,
                                        parent:"system_manage"
                                    }
                    ],
                    icon: "glyphicon glyphicon-cog",
                    name: "系统管理",
                    type: '1',
                    paixu: 4
                }];
                var data = { data : $rootScope.datas};*/

                /*写死数据*/
                $rootScope.mencs = [];
                for (var i = 0; i < data.children.length; i++) {
                    for (var j = 0; j < data.children[i].children.length; j++) {
                        $rootScope.mencs.push(data.children[i].children[j].url);
                        if ("/"+data.children[i].children[j].url == next.url) {
                            is_can = true;
                            data.children[i].active = true;
                            data.children[i].current = true;
                            data.children[i].child_open = true;
                            data.children[i].children[j].active = true;
                        }
                    }
                    if ("/"+data.children[i].url == next.url) {
                        is_can = true;
                        data.children[i].active = true;
                        data.children[i].current = true;
                    }
                }
                // if (!is_can) {
                //     $location.path("/home");
                // }
                 });
            } else {
                for (var i = 0; i < $rootScope.mencs.length; i++) {
                    if ("/"+$rootScope.mencs[i] == next.url) {
                        is_can = true;
                    }
                }
                // if (!is_can) {
                //     $location.path("/home");
                // }
            }
        });
    }
]);
// ngRoute提供的route模块的配置
backHomeApp.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/customerManage");
    $stateProvider
        .state('customerManage', {
            url: "/customerManage",
            templateUrl: "page/customer/customerManage.html"
        })
        .state('brandStatistics', {
            url: "/brandStatistics",
            templateUrl: "page/chart/brandStatistics.html"
        })
        .state('storeStatistics', {
            url: "/storeStatistics",
            templateUrl: "page/chart/storeStatistics.html"
        })
        .state('saleStatistics', {
            url: "/saleStatistics",
            templateUrl: "page/chart/saleStatistics.html"
        })
        .state('brandManage', {
            url: "/brandManage",
            templateUrl: "page/basic/brandManage.html"
        })
        .state('carManage', {
            url: "/carManage",
            templateUrl: "page/basic/carManage.html"
        })
        .state('storeManage', {
            url: "/storeManage",
            templateUrl: "page/basic/storeManage.html"
        })
        .state('salesmanManage', {
            url: "/salesmanManage",
            templateUrl: "page/basic/salesmanManage.html"
        })
        .state('userManage', {
            url: "/userManage",
            templateUrl: "page/system/userManage.html"
        })
        .state('itemManage', {
            url: "/itemManage",
            templateUrl: "page/system/itemManage.html"
        })
        .state('roleManage', {
            url: "/roleManage",
            templateUrl: "page/system/roleManage.html"
        })
});

/**
 * 配置angular.js的$http服务，解决使用post提交表单时后台获取不到数据的问题。
 *
 * @see http://my.oschina.net/buwei/blog/191640
 * @param  {String}
 * @return {[type]}
 */
backHomeApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        /**
         * The workhorse; converts an object to x-www-form-urlencoded serialization.
         * @param {Object} obj
         * @return {String}
         */
        var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;

            for (name in obj) {
                value = obj[name];

                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
                }
            }

            return query.length ? query.substr(0, query.length - 1) : query;
        };

        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
}]);
// };